﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using cwtrock.model;
using cwtrock.service.impl;
using System.Collections;

namespace cwtrock.view.handle
{
    public partial class GeneralRpt : Form
    {
        private VoucherServiceImpl voucherServiceImpl = new VoucherServiceImpl();
        private AccountsServiceImpl accountServiceImpl = new AccountsServiceImpl();
        private static GeneralRpt frm;
        private Vouchers voucher_begin;   
        private Vouchers voucher_end;
        private Vrecords vrecord_begin;
        private Vrecords vrecord_end;
        private Dictionary<string, List<Vrecords>> vrdict;
        private string duration_general;


        #region 单体模式
        /// <summary>
        /// 单体模式
        /// </summary>
        /// <returns></returns>
        public static GeneralRpt getInstance()
        {
            if (frm == null)
            {
                frm = new GeneralRpt();

            }

            return frm;
        }
        #endregion

        public GeneralRpt()
        {
            InitializeComponent();

            //这是了隔行换色的代码  
            this.dgv_General.RowsDefaultCellStyle.BackColor = Color.FromArgb(255, 255, 255);//第一行  
            this.dgv_General.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(244, 247, 252);//第二行  
            this.dgv_General.GridColor = Color.FromArgb(128, 128, 128);//字段网格边框  
            


            // 去除表头排序
            for (int i = 0; i < dgv_General.Columns.Count; i++)
            {
                dgv_General.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
        }

        #region 初始化 总账报表的数据
        // 初始化 总账报表的数据
        public void initGeneralRptData()
        {

           vrdict = voucherServiceImpl.getVrecordsByGeneral(voucher_begin, voucher_end, vrecord_begin, vrecord_end);

           if (vrdict == null)
              return;

        //  dgv_General.ColumnHeadersHeight = 40;
        //  dgv_General.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
       //   dgv_General.MergeColumnNames.Add("remains");
      //    dgv_General.AddSpanHeader(6,2,"余额");


          foreach (KeyValuePair<string, List<Vrecords>> vrpair in vrdict) 
         {
             string acctcode = (string)vrpair.Key;
             List<Vrecords> vrls = vrpair.Value;
           if (vrls != null && vrls.Count > 0) {

               int count = 0;
               int rows = dgv_General.Rows.Count-1;
               Vrecords vrecord = vrls[0];

               for (; count < 4;count++ )
               {
                  
                   if (count == 0)
                   {

                       dgv_General.Rows.Add();
                       int j = 0;
                       int crow = rows + count;
                       dgv_General.Rows[crow].Cells[j++].Value = acctcode;
                       dgv_General.Rows[crow].Cells[j++].Value = vrecord.Acctname;
                       dgv_General.Rows[crow].Cells[j++].Value = acctcode;
                       dgv_General.Rows[crow].Cells[j++].Value = vrecord.Acctname;
                       dgv_General.Rows[crow].Cells[j++].Value = "";
                       dgv_General.Rows[crow].Cells[j++].Value = "期初余额";
                       dgv_General.Rows[crow].Cells[j++].Value = "";
                       dgv_General.Rows[crow].Cells[j++].Value = "";
                       dgv_General.Rows[crow].Cells[j++].Value = "平";
                   }
                   else if (count == 1)
                   {
                       VoucherGeneral vg = calcVouchers(vrls);
                       dgv_General.Rows.Add();
                       int j = 0;
                       int crow = rows + count;
                       dgv_General.Rows[crow].Cells[j++].Value = acctcode;
                       dgv_General.Rows[crow].Cells[j++].Value = vrecord.Acctname;
                       dgv_General.Rows[crow].Cells[j++].Value = "";
                       dgv_General.Rows[crow].Cells[j++].Value = "";

                       string voucode = "";
                       Dictionary<string, List<int>> vgdict = vg.CatlogVouls;
                       if (vgdict != null)
                       {

                           foreach (KeyValuePair<string, List<int>> vg1 in vgdict)
                           {
                               string key = vg1.Key;
                               string value = "";
                               List<int> ls = vg1.Value;
                               if (ls != null && ls.Count > 0)
                               {

                                   if (ls.Count == 1)
                                   {
                                       value = ls[0] + "";
                                   }
                                   else
                                   {

                                       value = ls[0] + "～" + ls[ls.Count - 1];
                                   }

                               }

                               voucode += key + value + "  ";

                           }
                       }
                       dgv_General.Rows[crow].Cells[j++].Value = voucode;
                       dgv_General.Rows[crow].Cells[j++].Value = "本期合计";
                       dgv_General.Rows[crow].Cells[j++].Value = vg.Bamount + ".00";
                       dgv_General.Rows[crow].Cells[j++].Value = vg.Lamount + ".00";
                       decimal remains = vg.Bamount - vg.Lamount;
                       string remains_state = "";
                       if (remains > 0)
                           remains_state = "借";
                       else if (remains < 0)
                           remains_state = "贷";
                       else
                           remains_state = "平";
                       dgv_General.Rows[crow].Cells[j++].Value = remains_state;
                       dgv_General.Rows[crow].Cells[j++].Value = System.Math.Abs(remains) + ".00";

                   }

                   else if (count == 2)
                   {
                       dgv_General.Rows.Add();
                       int j = 0;
                       int crow = rows + count;
                       dgv_General.Rows[crow].Cells[j++].Value = acctcode;
                       dgv_General.Rows[crow].Cells[j++].Value = vrecord.Acctname;
                       dgv_General.Rows[crow].Cells[j++].Value = "";
                       dgv_General.Rows[crow].Cells[j++].Value = "";
                       dgv_General.Rows[crow].Cells[j++].Value = "";
                       dgv_General.Rows[crow].Cells[j++].Value = "本年累计";
                       dgv_General.Rows[crow].Cells[j++].Value = dgv_General.Rows[crow - 1].Cells[6].Value;
                       dgv_General.Rows[crow].Cells[j++].Value = dgv_General.Rows[crow - 1].Cells[7].Value;
                       dgv_General.Rows[crow].Cells[j++].Value = dgv_General.Rows[crow - 1].Cells[8].Value;
                       dgv_General.Rows[crow].Cells[j++].Value = dgv_General.Rows[crow - 1].Cells[9].Value;

                   }

                   else if (count == 3)
                   {
                       dgv_General.Rows.Add();
                       int j = 0;
                       int crow = rows + count;
                       dgv_General.Rows[crow].Cells[j++].Value = acctcode;
                       dgv_General.Rows[crow].Cells[j++].Value = vrecord.Acctname;
                       dgv_General.Rows[crow].Cells[j++].Value = "";
                       dgv_General.Rows[crow].Cells[j++].Value = "";
                       dgv_General.Rows[crow].Cells[j++].Value = "";
                       dgv_General.Rows[crow].Cells[j++].Value = "结转下年";
                       dgv_General.Rows[crow].Cells[j++].Value = "";
                       dgv_General.Rows[crow].Cells[j++].Value = "";
                       dgv_General.Rows[crow].Cells[j++].Value = dgv_General.Rows[crow - 1].Cells[8].Value;
                       dgv_General.Rows[crow].Cells[j++].Value = dgv_General.Rows[crow - 1].Cells[9].Value;

                   }
               }
          
           }

         }

          dgv_General.Focus();
         if(dgv_General.Rows.Count>0)          
         {
             dgv_General.CurrentRow.Selected = false;
             dgv_General.Rows[0].Selected = true;
         }


        }
        #endregion

        public void writeDuration(string duration) {

            this.duration_general = duration;
        }

        private VoucherGeneral calcVouchers(List<Vrecords> vrls)
        {
            if (vrls == null)
                return null;

            Dictionary<string, List<int>> vgdict = new Dictionary<string, List<int>>();
            decimal bamount =0;
            decimal lamount=0;

            VoucherGeneral vg = new VoucherGeneral();

            foreach(Vrecords vr in vrls){

                List<int> list;

                if (!vgdict.ContainsKey(vr.Voucher.Vtypename))
                {

                    list = new List<int>();
                    list.Add(vr.Voucher.Vcode);
                    vgdict.Add(vr.Voucher.Vtypename,list);

                }

                else {

                    list = vgdict[vr.Voucher.Vtypename];
                    list.Add(vr.Voucher.Vcode);
                    vgdict[vr.Voucher.Vtypename] = list;
                
                }

                bamount += vr.Vrbamount;
                lamount += vr.Vrlamount;

                vg.CatlogVouls = vgdict;
                vg.Bamount = bamount;
                vg.Lamount = lamount;

            }

            return vg;
        }


        private void close_tbtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void GeneralRpt_FormClosing(object sender, FormClosingEventArgs e)
        {
            frm = null;
        }

        public Vouchers Voucher_end
        {
            get { return voucher_end; }
            set { voucher_end = value; }
        }

        public Vouchers Voucher_begin
        {
            get { return voucher_begin; }
            set { voucher_begin = value; }
        }

        public Vrecords Vrecord_end
        {
            get { return vrecord_end; }
            set { vrecord_end = value; }
        }

        public Vrecords Vrecord_begin
        {
            get { return vrecord_begin; }
            set { vrecord_begin = value; }
        }

        private void dgv_General_DoubleClick(object sender, EventArgs e)
        {
            if (dgv_General.SelectedRows.Count == 0)
            {
                return;
            }
           string acctcode= dgv_General.SelectedRows[0].Cells[0].Value.ToString();
           string acctname = dgv_General.SelectedRows[0].Cells[1].Value.ToString();
           List<Vrecords> vrls = vrdict[acctcode];

           DetailRpt detailRpt = new DetailRpt();
           detailRpt.initFrm("(" + acctcode+")"+acctname, duration_general, vrls);
           detailRpt.MdiParent = this.MdiParent;
           detailRpt.Show();

        }
    }
}
